package com.zwh.modules.scheduler.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * 项目任务到期提醒通知
 * @author doubleZhang
 */
public class ProjectTaskNotifyJob implements Job {
//    @Autowired
//    TaskService taskService;
//    @Autowired
//    ProjectService projectService;
//    @Autowired
//    DingTalkService dingTalkService;
//    @Autowired
//    DingTalkMsgService dingTalkMsgService;
//    @Autowired
//    UserService userService;
//    @Autowired
//    TaskUserService taskUserService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("项目任务到期提醒通知任务执行");
        //获取当前日期
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        Date curDate = new Date();
//        String curentDateString = simpleDateFormat.format(curDate);
//        // 离任务到期日期还有3天时进行提醒，只提醒未完成任务
//        List<TaskExpireOrPassDto> taskExpireOrPassDtoList =  taskService.listTaskByWillExpireTimeAndUnfinish(curentDateString);
//        if(taskExpireOrPassDtoList==null||taskExpireOrPassDtoList.isEmpty()){
//            return;
//        }
//        //根据项目id分组
//        Map<String, List<TaskExpireOrPassDto>> projectIdMapList  = taskExpireOrPassDtoList.stream()
//                .collect(Collectors.groupingBy(TaskExpireOrPassDto::getProjectId));
//        for(Map.Entry<String,List<TaskExpireOrPassDto>> entry:projectIdMapList.entrySet()){
//            String projectId = entry.getKey();
//            List<TaskExpireOrPassDto> taskDtoItemList = entry.getValue();
//            // 发送消息通知
//            Project project = projectService.get(projectId);
//            for(TaskExpireOrPassDto dtoItem:taskDtoItemList){
//                /**
//                 根据任务id和项目id，更新消息通知的统计状态
//                 发送新的消息通知时，把事件对应的旧任务id的通知设置为不加入统计
//                 */
//                if(dtoItem.getId().equals("1244464829735985152")) {
//                    if(dtoItem.getEnd()==null){
//                        continue;
//                    }
//                    sendProjectTaskExpireMsg(project, dtoItem);
//                }
//            }
//        }
    }
    /**
     * 发送任务到期提醒通知
     * @param project
     */
//    private void sendProjectTaskExpireMsg(Project project,TaskExpireOrPassDto taskExpireOrPassDto){
//        String receiveUserCode = null;
//        String msgSendName="任务到期提醒";
//        //获取任务用户
//        TaskUser taskUser = taskUserService.getTaskUserOne(taskExpireOrPassDto.getId());
//        if(taskUser.getUserCode().equals("0")){
//            receiveUserCode=project.getProjectManagerCode();
//        }else{
//            receiveUserCode=taskUser.getUserCode();
//        }
//
//        // 钉钉消息推送-立项申请
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
//        Date sendDate = new Date();
//        String sendContent = "项目任务情况通知" + DingTalkMsgBusConstant.LINE_BREAK_STR + project.getProjectName()
//                + DingTalkMsgBusConstant.LINE_BREAK_STR + taskExpireOrPassDto.getName()+"任务,还有"+taskExpireOrPassDto.getExpireDay()+"天将要到期，请尽快处理！！！"
//                + DingTalkMsgBusConstant.LINE_BREAK_STR + "任务到期时间：" + sdf.format(taskExpireOrPassDto.getEnd());
//
//        User user = userService.get(receiveUserCode);
//        if(user==null){
//            return;
//        }
//        DingTalkMsg dingTalkMsg= DingTalkUtils.genProjectTaskNotifyDingTalkMsg(DingTalkMsgBusConstant.MSG_TYPE_TASK_EXPIRE,
//                project.getId(), project.getProjectName(),taskExpireOrPassDto.getId(),
//                msgSendName, sendContent, receiveUserCode,sendDate);
//
//        // 发送立项审核通知时，要将旧的立项审核通知 统计状态设置为不统计
//        // 根据业务id和子业务id还有消息类型 获取旧的消息通知数据
//        DingTalkMsg updateMsg = new DingTalkMsg();
//        updateMsg.setBusStat(DingTalkMsgBusConstant.BUS_STAT_1);
//        updateMsg.setBusId(dingTalkMsg.getBusId());
//        updateMsg.setBusChildId(taskExpireOrPassDto.getId());
//        updateMsg.setNotifyType(DingTalkMsgBusConstant.NOTIFY_TYPE_NOTIFY);
//        updateMsg.setBusType(DingTalkMsgBusConstant.BUS_TYPE_PROJECT);
//        updateMsg.setMsgType(DingTalkMsgBusConstant.MSG_TYPE_TASK_EXPIRE);
//        updateMsg.setMsgTypeOperation("");
//        //设置旧数据为不加入统计
//        dingTalkMsgService.updateMsgBusStateByEntity(updateMsg);
//        //发送消息通知
//        dingTalkService.sendDingTalkTextMsg(dingTalkMsg);
//    }
}
